/*
 * @Author: 彭林
 * @Date: 2021-05-14 18:19:20
 * @LastEditTime: 2021-07-01 15:06:01
 * @LastEditors: 彭林
 * @Description:
 * @FilePath: /src/saas-app/app/api/v1/class_member.go
 */
package v1

import (
	"saas-app/app/define"
	"saas-app/app/model"
	"saas-app/app/service"
	"saas-app/library/response"

	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

func GetClassMemeberList(r *ghttp.Request) {

	var (
		ClassMemeberListParams *define.ClassMemeberListParams
	)

	if err := r.Parse(&ClassMemeberListParams); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	result, total := service.ClassMemberService().GetClassMemeberList(ClassMemeberListParams)
	response.JsonExit(r, 0, "ok", response.Paging(result, total, ClassMemeberListParams.Page, ClassMemeberListParams.Limit))
}

func CreateClassMemeber(r *ghttp.Request) {

	var (
		data                   *define.CreateClassMemberRequest
		CreateClassMemberParam *define.CreateClassMemberParam
	)

	if err := r.Parse(&data); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	if err := gconv.Struct(data, &CreateClassMemberParam); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	var classMember model.ClassMember
	if err := gconv.Struct(CreateClassMemberParam, &classMember); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	result := service.ClassMemberService().CreateClassMember(&classMember)
	response.JsonExit(r, 0, "ok", result)
}

func LeaveClassMember(r *ghttp.Request) {

	student_id := r.GetQueryUint("student_id")
	class_id := r.GetQueryUint("class_id")
	status := r.GetQueryUint("status")

	result := service.ClassMemberService().LeaveClassMember(class_id, student_id, status)
	response.JsonExit(r, 0, "ok", result)
}
